package com.cyt.work.wb.service.impl;

import cn.hutool.core.date.DateUtil;
import com.cyt.base.model.dto.ReturnResult;
import com.cyt.base.util.StringUtils;
import com.cyt.base.util.BaseConstant;
import com.cyt.base.util.DictCode;
import com.cyt.base.util.ErrorCode;
import com.cyt.work.util.cache.WbDictCode;
import com.cyt.work.wb.dao.WbRepairDao;
import com.cyt.work.wb.model.po.WbRepair;
import com.cyt.work.wb.model.po.WbUser;
import com.cyt.work.wb.model.vo.WbRepairVo;
import com.cyt.work.wb.service.WbRepairService;
import com.cyt.sys.model.po.SysUser;
import com.cyt.sys.service.impl.BaseServiceImpl;
import com.cyt.wx.model.po.WxMember;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 维保模块-报修单表Service层实现类
 *
 * @author shengQiang Yu
 * @date 2019-11-28 9:27:23
 */
@Service("wbRepairService")
public class WbRepairServiceImpl extends BaseServiceImpl<WbRepair> implements WbRepairService {

    @Resource
    private WbRepairDao wbRepairDao;

    @Autowired
    public void setBaseDao(WbRepairDao wbRepairDao) {
        this.baseDao = wbRepairDao;
    }

    /**
     * 查询对象集合
     *
     * @param wbRepairVo 对象
     * @return List 对象集合
     * @author shengQiang Yu
     * @date 2019-11-28 9:27:23
     */
    @Override
    public List<WbRepair> listExample(WbRepairVo wbRepairVo) {
        Example example = new Example(WbRepair.class);
        example.setOrderByClause(" update_time desc");
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(wbRepairVo.getApplyId())) {
            criteria.andEqualTo("applyId", wbRepairVo.getApplyId());
        }
        if (StringUtils.isNotBlank(wbRepairVo.getWbUserId())) {
            criteria.andEqualTo("wbUserId", wbRepairVo.getWbUserId());
        }
        if (StringUtils.isNotBlank(wbRepairVo.getStatus())) {
            criteria.andEqualTo("status", wbRepairVo.getStatus());
        }
        if (StringUtils.isNotBlank(wbRepairVo.getRepairType())) {
            criteria.andEqualTo("repairType", wbRepairVo.getRepairType());
        }
        criteria.andEqualTo("removeTag", DictCode.BASE_REMOVE_TAG_NORMAL);
        return wbRepairDao.selectByExample(example);
    }

    /**
     * 分页查询
     *
     * @param wbRepairVo 接受参数对象
     * @param pageNum          分页数
     * @param pageSize         每页显示条数
     * @return ReturnResult
     * @author shengQiang Yu
     * @date 2019-11-28 9:27:23
     */
    @Override
    public ReturnResult page(WbRepairVo wbRepairVo, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        PageHelper.orderBy(" create_time desc");
        List<WbRepair> list = listExample(wbRepairVo);
        PageInfo<WbRepair> page = new PageInfo<>(list);
        return new ReturnResult(page.getList(), page.getTotal(), page.isHasNextPage());
    }

    /**
     * 添加或更新
     *
     * @param wbRepairVo 参数接受对象
     * @param sysUser      当前用户
     * @return ReturnResult
     * @author shengQiang Yu
     * @date 2019-11-28 9:27:23
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult addOrUpdate(WbRepairVo wbRepairVo, SysUser sysUser) {
        if (StringUtils.isBlank(wbRepairVo.getId())) {
            WbRepair wbRepair = new WbRepair(sysUser);
            wbRepair.setWbUserId(wbRepairVo.getWbUserId());
            wbRepair.setApplyId(wbRepairVo.getApplyId());
            wbRepair.setRepairType(wbRepairVo.getRepairType());
            wbRepair.setDeviceId(wbRepairVo.getDeviceId());
            wbRepair.setDeviceName(wbRepairVo.getDeviceName());
            wbRepair.setDeviceNum(wbRepairVo.getDeviceNum());
            wbRepair.setDeviceModel(wbRepairVo.getDeviceModel());
            wbRepair.setRoomId(wbRepairVo.getRoomId());
            wbRepair.setRoomName(wbRepairVo.getRoomName());
            wbRepair.setImages(wbRepairVo.getImages());
            wbRepair.setDescription(wbRepairVo.getDescription());
            wbRepair.setStatus(wbRepairVo.getStatus());
            wbRepair.setRepairmanId(wbRepairVo.getRepairmanId());
            wbRepair.setRepaimanName(wbRepairVo.getRepaimanName());
            wbRepair.setRepaimanPhone(wbRepairVo.getRepaimanPhone());
            wbRepairDao.insert(wbRepair);
            return new ReturnResult(BaseConstant.DATABASE_SUCCEED_INSERT);
        } else {
            WbRepair wbRepair = wbRepairDao.selectByPrimaryKey(wbRepairVo.getId());
            wbRepair.setWbUserId(wbRepairVo.getWbUserId());
            wbRepair.setApplyId(wbRepairVo.getApplyId());
            wbRepair.setRepairType(wbRepairVo.getRepairType());
            wbRepair.setDeviceId(wbRepairVo.getDeviceId());
            wbRepair.setDeviceName(wbRepairVo.getDeviceName());
            wbRepair.setDeviceNum(wbRepairVo.getDeviceNum());
            wbRepair.setDeviceModel(wbRepairVo.getDeviceModel());
            wbRepair.setRoomId(wbRepairVo.getRoomId());
            wbRepair.setRoomName(wbRepairVo.getRoomName());
            wbRepair.setImages(wbRepairVo.getImages());
            wbRepair.setDescription(wbRepairVo.getDescription());
            wbRepair.setStatus(wbRepairVo.getStatus());
            wbRepair.setRepairmanId(wbRepairVo.getRepairmanId());
            wbRepair.setRepaimanName(wbRepairVo.getRepaimanName());
            wbRepair.setRepaimanPhone(wbRepairVo.getRepaimanPhone());
            wbRepair.setUpdateTime(new Date());
            wbRepair.setUpdateUser(sysUser.getId());
            wbRepairDao.updateByPrimaryKey(wbRepair);
            return new ReturnResult(BaseConstant.DATABASE_SUCCEED_UPDATE);
        }
    }

    /**
     * 根据条件逻辑删除
     *
     * @param wbRepairVo 参数接受对象
     * @param sysUser 当前用户
     * @return ReturnResult
     * @author shengQiang Yu
     * @date 2019-11-28 9:27:23
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult remove(WbRepairVo wbRepairVo, SysUser sysUser) {
        //要修改的字段
        WbRepair wbRepair = new WbRepair();
        wbRepair.setRemoveTag(DictCode.BASE_REMOVE_TAG_DELETE);
        wbRepair.setUpdateUser(sysUser.getId());
        wbRepair.setUpdateTime(new Date());
        //筛选条件
        Example example = new Example(WbRepair.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(wbRepairVo.getId())) {
            criteria.andIn("id", Arrays.asList(wbRepairVo.getId().split(",")));
        }
        criteria.andEqualTo("removeTag", DictCode.BASE_REMOVE_TAG_NORMAL);
        wbRepairDao.updateByExampleSelective(wbRepair, example);
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }

    /**
     * 根据条件物理删除
     *
     * @param wbRepairVo 参数接受对象
     * @param sysUser      当前用户
     * @return ReturnResult
     * @author shengQiang Yu
     * @date 2019-11-28 9:27:23
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult delete(WbRepairVo wbRepairVo, SysUser sysUser) {
        //筛选条件
        Example example = new Example(WbRepair.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(wbRepairVo.getId())) {
            criteria.andIn("id", Arrays.asList(wbRepairVo.getId().split(",")));
        }
        wbRepairDao.deleteByExample(example);
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }

    /**
     * 报修申请
     *
     * @param wbRepairVo 参数接受对象
     * @param wbUser   当前用户
     * @return ReturnResult
     * @author shengQiang Yu
     * @date 2019-11-28 9:27:23
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult createRepair(WbRepairVo wbRepairVo, WbUser wbUser) {
        WbRepair wbRepair = new WbRepair(null);
        wbRepair.setWbUserId(wbUser.getId());
        wbRepair.setApplyId(wbRepairVo.getApplyId());
        wbRepair.setServicerId(wbRepairVo.getServicerId());
        wbRepair.setRepairType(wbRepairVo.getRepairType());
        wbRepair.setDeviceId(wbRepairVo.getDeviceId());
        wbRepair.setDeviceName(wbRepairVo.getDeviceName());
        wbRepair.setDeviceNum(wbRepairVo.getDeviceNum());
        wbRepair.setDeviceModel(wbRepairVo.getDeviceModel());
        wbRepair.setRoomId(wbRepairVo.getRoomId());
        wbRepair.setRoomName(wbRepairVo.getRoomName());
        wbRepair.setImages(wbRepairVo.getImages());
        wbRepair.setDescription(wbRepairVo.getDescription());
        wbRepair.setStatus(WbDictCode.WB_REPAIR_STATUS_DFP);
        wbRepair.setRepairmanId(wbRepairVo.getRepairmanId());
        wbRepair.setRepaimanName(wbRepairVo.getRepaimanName());
        wbRepair.setRepaimanPhone(wbRepairVo.getRepaimanPhone());
        int num=wbRepairDao.insert(wbRepair);
        if(num>0){
            return new ReturnResult(BaseConstant.DATABASE_SUCCEED_INSERT);
        }
        return new ReturnResult(ErrorCode.CODE_1,ErrorCode.CODE_1_MSG);
    }
}